//
//  FLMChatBaseCell.m
//  FLMSuperS
//
//  Created by chenshan on 2016/10/9.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMChatBaseCell.h"

@implementation FLMChatBaseCell

#pragma mark - override methods
- (void)layoutSubviews {
    [super layoutSubviews];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    if (editing) {
        [self buildEidtingFrame];
    }
    else {
        editingFrame = CGRectZero;
    }
    [super setEditing:editing animated:animated];
}

#pragma mark - publice methods
- (void)commitInit {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //long press recognizer need subclass to determine which view should be add, not do add action here

    _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectZero];
    self.avatarImgView.userInteractionEnabled = YES;
    
    _nicknameLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
    self.nicknameLabel.delegate = self;
    self.nicknameLabel.font = FLMFontContent;
    self.nicknameLabel.textColor = FLMColorContent;
    self.nicknameLabel.linkAttributes = @{NSForegroundColorAttributeName:FLMColorContent};
    _sendingIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectZero];
    self.sendingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    _errorImgView = [[UIImageView alloc] initWithFrame:CGRectZero];
    
    [self.contentView addSubview:self.avatarImgView];
    [self.contentView addSubview:self.editingView];
    [self.contentView addSubview:self.nicknameLabel];
    [self.contentView addSubview:self.sendingIndicator];
    [self.contentView addSubview:self.errorImgView];
    
    _bubbleMyself = [UIImage imageNamed:@"chat_sender_bg_buleRight"];
    _bubbleOther = [UIImage imageNamed:@"chat_sender_bg_grayLeft"];
    
    self.contentView.backgroundColor = FLMColorBackgroud;
}

- (void)buildEidtingFrame {
    editingFrame = CGRectMake(FLMCellPadding, FLMCellPadding, FLMCellEditingEdgeLength, FLMCellEditingEdgeLength);
}

- (void)buildMyselfAvatarFrame {
    //avatar frame
    avatareFrame = CGRectMake(FLMScreenW - FLMCellPadding - FLMAvatarEdgeLength,
                              FLMCellPadding, FLMAvatarEdgeLength, FLMAvatarEdgeLength);
}

- (CGSize)buildMyselfNicknameFrame {
    //nickname frame
    CGSize nicknameSize = CGSizeZero;
    if (self.isShowNickname) {
        self.nicknameLabel.textAlignment = NSTextAlignmentRight;
        nicknameSize = [self getNicknameSize];
        
        nicknameFrame = CGRectMake(FLMScreenW - FLMAvatarEdgeLength - 2 * FLMCellPadding - nicknameSize.width - FLMBubbleArrowWidth,
                                   FLMCellPadding, nicknameSize.width, nicknameSize.height);
    } else {
        nicknameFrame = CGRectZero;
    }
    return nicknameSize;
}

- (void)buildOtherAvatarFrame {
    
    avatareFrame = CGRectMake(FLMCellPadding, FLMCellPadding, FLMAvatarEdgeLength, FLMAvatarEdgeLength);
    if (self.isEditing) avatareFrame = CGRectMake(avatareFrame.origin.x + FLMCellEditingEdgeLength + FLMCellPadding, FLMCellPadding, FLMAvatarEdgeLength, FLMAvatarEdgeLength);
    
}

- (CGSize)buildOtherNicknameFrame {
    //nickname frame
    CGSize nicknameSize = CGSizeZero;
    if (self.isShowNickname) {
        self.nicknameLabel.textAlignment = NSTextAlignmentRight;
        nicknameSize = [self getNicknameSize];
        
        nicknameFrame = CGRectMake(FLMAvatarEdgeLength + 2 * FLMCellPadding + FLMBubbleArrowWidth,
                                   FLMCellPadding, nicknameSize.width, nicknameSize.height);
        if (self.isEditing) nicknameFrame = CGRectMake(nicknameFrame.origin.x + FLMCellEditingEdgeLength + FLMCellPadding, nicknameFrame.origin.y, nicknameFrame.size.width, nicknameFrame.size.height);
    } else {
        nicknameFrame = CGRectZero;
    }
    return nicknameSize;
}

- (CGSize)getNicknameSize {
    CGSize result = [TTTAttributedLabel sizeThatFitsAttributedString:self.nicknameLabel.attributedText withConstraints:CGSizeMake(FLMScreenW - FLMAvatarEdgeLength - 2 * FLMCellPadding, 99999) limitedToNumberOfLines:99999];
    result = CGSizeMake(ceil(result.width), ceil(result.height));
    
    return result;
}

- (void)UIPrepareForSendingDataStatus {
    NSLog(@"super void implement, you must override this method!!");
}

- (void)UIPrepareForCreateStatus; {
    NSLog(@"super void implement, you must override this method!!");
}

- (void)UIPrepareForNormalStatus {
    NSLog(@"super void implement, you must override this method!!");
}

- (void)UIPrepareForErrorStatus {
    NSLog(@"super void implement, you must override this method!!");
}

- (CGFloat)getHeight {
    NSLog(@"super void implement, you must override this method!!");
    return 0;
}
@end
